
var qs=require('qs');
var request=require('request');
var fs=require('fs');
var acessPath=process.cwd()+'/data/access_token';


var queryParams = {
    'grant_type':'client_credential',
    'appid':'wx239b298be0b923e9',
    'secret':'85f7471079e3358d4cca3f27dc9fa9a8'
  };
var wxGetAccessTokenBaseUrl='https://api.weixin.qq.com/cgi-bin/token?'+qs.stringify(queryParams);
var options = {
    method: 'GET',
    url: wxGetAccessTokenBaseUrl
  };

var GetAccessToken=function(){
	request(options, function (err, res, body) {
      if (res) {
        var token = JSON.parse(body)['access_token'];
	    fs.writeFile(acessPath, token,'utf-8', function (err) {
	      
	    });
      } else {
        
      }
    });
}


module.exports.startTokenCycle=function(){
	GetAccessToken();
	setInterval(function(){
		GetAccessToken();
	},7000*1000);
}

module.exports.getAccessTokenPath=function(){
	return acessPath;
}